﻿using UnityEngine;

class CameraControl : MonoBehaviour
{
    private GameObject player;

    private Vector3 offset;

    private void Awake()
    {
        player = GameObject.FindGameObjectWithTag("Player");
    }

    private void Start()
    {
        offset = player.transform.position;
    }

    private void LateUpdate()
    {
        //摄像机跟踪
        transform.position += player.transform.position - offset;
        offset = player.transform.position;

        if (Input.GetMouseButton(1))
        {//右键鼠标摄像机旋转
            Cursor.lockState = CursorLockMode.Locked;
            float mouseX = Input.GetAxis("Mouse X");

            transform.RotateAround(player.transform.position, Vector3.up, mouseX * 10);
        }
        else
        {
            Cursor.lockState = CursorLockMode.None;
        }


    }

    //缩放
    private void Scale()
    {
        float dis = offset.magnitude;//向量长度
        dis += Input.GetAxis("Mouse ScrollWheel") * 5;
        if (dis < 10 || dis > 40)
            return;
        offset = offset.normalized * dis;//单位向量
    }
    
}
